package com.banmao.mall.config;

import com.banmao.mall.realm.AdminRealm;
import com.banmao.mall.realm.ClientRealm;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Map;

@Configuration
public class ShiroConfig {

    // 1. ShiroFilterFactoryBean
    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(@Qualifier("defaultSecurityManager") DefaultWebSecurityManager securityManager) {
        ShiroFilterFactoryBean bean = new ShiroFilterFactoryBean();
        // 1. 设置安全管理器
        bean.setSecurityManager(securityManager);

        bean.setLoginUrl("/admin/unauth");

        // 2. 添加Shiro的内置过滤器
        /*
            anno：无需认证就可以访问
            authc：必须认证才能访问
            user：必须拥有 记住我 才能访问
            perms：拥有对某个资源的权限才能访问
            role：拥有某个角色权限才能访问
         */
        Map<String, String> filterMap = new LinkedHashMap<>();

        filterMap.put("/admin/auth/login", "anon");
        filterMap.put("/admin/unauthc", "anon");
        filterMap.put("/admin/auth/*", "authc");

        /*
            将其余所有请求列入需要认证通过
            因为不方便测试所有在此先注释起来
         */
        //filterMap.put("/admin/**", "authc");

        bean.setFilterChainDefinitionMap(filterMap);

        return bean;
    }

    // 2. DefaultWebSecurityManager
    @Bean("defaultSecurityManager")
    public DefaultWebSecurityManager securityManager(@Qualifier("adminRealm") AdminRealm adminRealm,
                                                     @Qualifier("clientRealm") ClientRealm clientRealm,
                                                     @Qualifier("webSessionManager") DefaultWebSessionManager webSessionManager,
                                                     CustomAuthenticator authenticator) {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        // 关联AuthRealm
        //securityManager.setRealm(adminRealm);

        securityManager.setSessionManager(webSessionManager);

        securityManager.setAuthenticator(authenticator);

        ArrayList<Realm> realms = new ArrayList<Realm>();
        realms.add(adminRealm);
        realms.add(clientRealm);
        securityManager.setRealms(realms);


        return securityManager;
    }

    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(@Qualifier("defaultSecurityManager") DefaultWebSecurityManager securityManager) {
        AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
        authorizationAttributeSourceAdvisor.setSecurityManager(securityManager);
        return authorizationAttributeSourceAdvisor;
    }

    //@Bean
    //public CustomAuthenticator authenticator(AdminRealm adminRealm, ClientRealm clientRealm) {
    //
    //    CustomAuthenticator customAuthenticator = new CustomAuthenticator();
    //
    //    ArrayList<Realm> realms = new ArrayList<Realm>();
    //    realms.add(adminRealm);
    //    realms.add(clientRealm);
    //    customAuthenticator.setRealms(realms);
    //
    //    return customAuthenticator;
    //}

    //@Bean
    //public

}
